package com.example.registration.service.impl;

import com.example.registration.dao.LoginMapper;
import com.example.registration.exception.DataBaseSqlException;
import com.example.registration.pojo.Admin;
import com.example.registration.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 *
 * @program: springboot_smbms
 * @Description: $
 * @Param: $
 * @return: $
 * @Author: xieyongde
 * @Date: 2019-08-15
 */
@Service
public class LoginServicelmap implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    public LoginMapper getLoginMapper() {
        return loginMapper;
    }

    public void setLoginMapper(LoginMapper loginMapper) {
        this.loginMapper = loginMapper;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    //登录
    public Map login(String name, String userPassword) throws DataBaseSqlException, Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if ((!name.equals("") && name != "") && (!userPassword.equals("") && userPassword != "")) {
            Admin admin = loginMapper.getUserByUserCode(name);
            if (admin != null) {
                if (userPassword.equals(admin.getPassword())) {
                    map.put("result", true);
                    map.put("msg", "登入成功");
                    map.put("data", admin);
                    map.put("id", admin.getAdmin_id());
                } else {
                    map.put("result", false);
                    map.put("msg", "密码不正确");
                }
            } else {
                map.put("result", false);
                map.put("msg", "没有该用户，必须是管理员才能登入该系统！");
            }
        } else {
            map.put("result", false);
            map.put("msg", "用户名或密码为空，请输入！");
        }
        return map;
    }

    //根据id查询管理员信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map showAdmin(Integer id) throws DataBaseSqlException, Exception {
        Admin admin = (Admin) loginMapper.showAdmin(id);
        Map<String, Object> map = new HashMap<String, Object>();
        if (admin != null) {
            map.put("msg", "查询成功");
            map.put("result", true);
            map.put("data", admin);
        } else {
            map.put("result", false);
            map.put("msg", "查询失败");
            throw new DataBaseSqlException("发送异常。。。请稍后再尝试！");
        }
        return map;
    }

    // 修改密码
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map updatepPassword(String newpassword, String oldpassword, String confirmpasswordint, Integer id) throws DataBaseSqlException, Exception {
        //判断密码是否一样
        Map map1 = showAdmin(id);
        Admin admin = (Admin) map1.get("data");
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (admin.getPassword().equals(oldpassword)) {
            if (newpassword.equals(confirmpasswordint)) {
                // 返回受影响的行数
                int result = loginMapper.updatepPassword(newpassword, id);
                // 定义HashMap集合
                if (result > 0) {
                    map.put("result", true);
                    map.put("msg", "修改密码成功");
                } else {
                    map.put("result", false);
                    map.put("msg", "修改密码失败");
                    throw new DataBaseSqlException("发送异常。。。请稍后再尝试！");
                }
            } else {
                map.put("result", false);
                map.put("msg", "修改密码失败，两次输入密码不一致");
            }
        } else {
            map.put("result", false);
            map.put("msg", "修改密码失败，旧密码输入不正确");
        }
        return map;
    }

}
